package com.woniuxy.threeprojcet.end_water_project_admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.threeprojcet.end_water_project_admin.domain.MeterReading;
import com.woniuxy.threeprojcet.end_water_project_admin.dao.MeterReadingMapper;
import com.woniuxy.threeprojcet.end_water_project_admin.domain.form.MeterReadingForm;
import com.woniuxy.threeprojcet.end_water_project_admin.service.IMeterReadingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 水量表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-08-26
 */
@Service
public class MeterReadingServiceImpl extends ServiceImpl<MeterReadingMapper, MeterReading> implements IMeterReadingService {
@Resource
private MeterReadingMapper meterReadingMapper;

    @Override
    public int addMeterReading( MeterReadingForm meterreadingForm) {
        MeterReading meterReading = new MeterReading();
        BeanUtils.copyProperties(meterreadingForm,meterReading);
        //获得上一次抄表时间
        Date lastMeterTime = lastMeterTimeByWaterMeterId(meterreadingForm.getWaterMeterId());

        if (null==lastMeterTime) {
            meterReading.setLastMeterTime(meterreadingForm.getMeterReadingTime());
        }else {//        把上一次抄表时间赋值
            meterReading.setLastMeterTime(lastMeterTime);
        }



//        System.out.println(meterReading.setLastMeterTime(lastMeterTime));
        int insert = meterReadingMapper.insert(meterReading);


        return insert;
    }

    @Override
    public IPage getMeterReading(IPage page) {
//        List<MeterReading> meterReadings = meterReadingMapper.selectList(new QueryWrapper<>());
        QueryWrapper queryWrapper = new QueryWrapper();
       queryWrapper.orderByDesc("meter_reading_id");
        IPage iPage = meterReadingMapper.selectMapsPage(page,queryWrapper );
        System.out.println(iPage.getRecords());
        return iPage;
    }

    //得到上次抄表时间
    @Override
    public Date lastMeterTimeByWaterMeterId(String waterMeterId) {
        System.out.println("waterMeterId:"+waterMeterId);
        Date lastMeterTime = meterReadingMapper.getLastMeterTimeByWaterMeterId(waterMeterId);

        System.out.println("lastMeterTime"+lastMeterTime);
        return lastMeterTime;
    }

    @Override
    public IPage searchMeterReading(IPage page, String waterMeterId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("meter_reading_id");
        queryWrapper.like("water_meter_id",waterMeterId);
        IPage iPage = meterReadingMapper.selectPage(page, queryWrapper);


        return iPage;
    }


}
